<?php


namespace app\system\controller;

class Account extends \app\common\Controller
{
	public function login()
	{
		$post = input("post.");
		$result = $this->validate($post, "app\\system\\validate\\Login");
		if ($result === true) {
			$login = \app\system\logic\LoginLogic::login($post);
			if (!\app\common\server\ConfigServer::get("website", "url", "")) {
				\app\common\server\ConfigServer::set("website", "url", getsiteurl());
			}
			$system = new \app\common\system\System();
			$system->check_auth(false);
			return json(["code" => \app\system\model\Code::success, "msg" => "登录成功", "data" => $login]);
		}
		return json(["code" => \app\system\model\Code::fail, "msg" => $result]);
	}
	public function logout()
	{
		$post = $this->request->post();
		if (empty($post["admin_id"]) || empty($post["admin_token"])) {
			return json(["code" => \app\system\model\Code::fail, "msg" => "缺少参数"]);
		}
		$admin = \think\Db::name("system_admin")->where(["id" => $post["admin_id"]])->find();
		if (empty($admin)) {
			return json(["code" => \app\system\model\Code::fail, "msg" => "不存在的管理员"]);
		}
		if ($admin["token"] != $post["admin_token"]) {
			return json(["code" => \app\system\model\Code::fail, "msg" => "token验证失败"]);
		}
		\think\Db::name("system_admin")->where(["id" => $admin["id"]])->update(["token" => md5(time())]);
		return json(["code" => \app\system\model\Code::success, "msg" => "已退出登录"]);
	}
	public function code()
	{
		return ShowCaptcha();
	}
}